package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.config.mq.NoticeTask;
<<<<<<< HEAD

import com.example.communist_app_api.db.dto.PhotoUserDetailDTO;
import com.example.communist_app_api.db.pojo.Organization;
import com.example.communist_app_api.db.pojo.Photo;
import com.example.communist_app_api.db.pojo.User;
import com.example.communist_app_api.enums.StatusEnum;
=======
import com.example.communist_app_api.db.dto.PhotoUserDetailDTO;
import com.example.communist_app_api.db.pojoDto.Organization;
import com.example.communist_app_api.db.pojoDto.User;
>>>>>>> 党建uniapp/dev
import com.example.communist_app_api.exception.GlobalException;
import com.example.communist_app_api.service.PhotoService;
import com.example.communist_app_api.service.UserOrganizationService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.PhotoShowReq;
import com.example.communist_app_api.vo.web.req.AuditPhotoFailReq;
import com.example.communist_app_api.vo.web.req.AuditPhotoReq;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/23 14:53
 */
@RestController
@RequestMapping("/photoAudit")
@Api("随手拍控制器")
public class PhotoAuditController {

    @Autowired
    private UserOrganizationService userOrganizationService;
    @Autowired
    private PhotoService photoService;
    @Autowired
    private NoticeTask noticeTask;
    @Autowired
    private UserService userService;

    @PostMapping("/showAllAuditPhoto")
    @ApiOperation("显示所有待审核的照片")
    public R showAllAuditPhoto(@RequestHeader("token") String token,@Valid @RequestBody PhotoShowReq req) {
        Integer userId = getUserIdByTokenAndWeb(token);
        //获取organizationId
        Organization organization = userOrganizationService.getOrganizationByUserId(userId);
        if (organization == null) {
            throw new GlobalException("当前用户未加入组织");
        }
        PageInfo<PhotoUserDetailDTO> pageInfo = photoService.selectAllPhotoByAudit(organization.getId(), req);

        return R.ok().put("pageInfo", pageInfo);
    }

    //审批
    @ApiOperation("图片审批成功")
    @PostMapping("/AuditPhoto/success")
    @Transactional
    public R AuditPersonalMessageAuditSuccess(@Valid @RequestBody AuditPhotoReq req, @RequestHeader("token") String token) {
        Integer photoId = req.getPhotoId();
        //web端登录的人
        Integer auditorId = getUserIdByTokenAndWeb(token);
        Integer status = photoService.updateAuditPhoto(photoId, auditorId);
        if (status == 0) {
            throw new GlobalException("更改信息失败");
        }
        String message = "";
        if (req.getMessage() == null) {
            message = "您提交的照片素材已审核成功，已上传到党建小程序随手拍区域!";
        } else {
            message = req.getMessage();
        }
        noticeTask.sendSystemNotice(String.valueOf(req.getUserId()), message);
        return R.ok("图片审核成功");
    }

    @ApiOperation("图片审批不合格")
    @PostMapping("/AuditPhoto/fail")
    @Transactional
    public R AuditPersonalMessageAuditFail(@Valid @RequestBody AuditPhotoFailReq req) {
        Integer photoId = req.getPhotoId();

        //随手拍图片有问题
        List<String> photoUrlList = req.getPhotoUrlList();
        String photoUrl = String.join(",", photoUrlList);

        String message = Optional.ofNullable(req.getMessage()+" 您提交的素材已审核失败，具体照片是：" + photoUrl + "请点击链接查看照片")
                .orElse("您提交的素材已审核失败，具体照片是：" + photoUrl + "请点击链接查看照片");

        noticeTask.sendSystemNotice(String.valueOf(req.getUserId()), message);
        //流程是：删除该条随手拍信息，并把审核不成功的信息返回给用户请求用户重新提交
        Integer deleteStatus = photoService.deletePhotoByPhotoId(photoId);
        if (deleteStatus == 0) {
            throw new GlobalException("此次更新删除失败！请重试！");
        }
        return R.ok("图片审核不通过");
    }

    private  Integer getUserIdByTokenAndUniApp(@RequestHeader("token") String token) {
        Claims claims = JwtUtils.parseJWT(token);
        String id_string = claims.getSubject();
        Integer id = Integer.parseInt(id_string);
        return id;
    }
    private  Integer getUserIdByTokenAndWeb(@RequestHeader("token") String token) {
        Claims claims = JwtUtils.parseJWT(token);
        String username = claims.getSubject();
        User userInfo = userService.getUserInfo(username);
        return userInfo.getId();
    }
}
